#include "TcpServer.h"

TcpServer::TcpServer(int port)
{
    this->m_port=port;
    initServer();
}

TcpServer::~TcpServer()
{
}

int TcpServer::getServerfd()
{
    return m_fd;
}

void TcpServer::initServer()
{
    m_fd = socket(AF_INET, SOCK_STREAM, 0);
    //initUserDate();

    // 端口复用
    int opt = 1;
    setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // 只需这一行！
    if (m_fd < 0)
    {
        perror("socket error");
        return;
    }
    // 绑定
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("192.168.139.128");
    addr.sin_port = htons(m_port);

    bind(m_fd, (struct sockaddr *)&addr, sizeof(addr));

    struct sockaddr_in clientaddr;
    socklen_t clientlen = sizeof(clientaddr);
    listen(m_fd, 128);

    this->m_epoll=new Epoll(this->m_fd);
    
}

void TcpServer::start()
{
    
    this->m_epoll->run();
}
